-- 定时器
local skynet = require "skynet"
local filelog = require "filelog"
local timer = require "timer"
local tabletool = require "tabletool"
local timetool = require "timetool"
require "imenum"

local filename = "talktimer.lua"

local json = require "cjson"
json.encode_sparse_array(true, 1, 1)

local TalkTimer = {}
local CMD = { }
local service = {}
local talkobj = nil
local talklogic = nil

--[Comment]
-- init
--
function TalkTimer.init(caller)
	service = caller
	talkobj = service.talker_data
	talklogic = service:get_logicbyname("talklogic")
end

--[Comment]
-- 处理发来的timer命令消息
--
function TalkTimer.timer( msgname, ...)
    local f = CMD[msgname]
    if f == nil then
        return
    end
    return f(...)
end
--[Comment]
-- 删除聊天对象
--
function CMD.delete_talk(timerid, request)
    filelog.sys_error("------timer.delete_talk------")
    if talkobj.delete_talk_timer_id == timerid then
        local timeout = 1
        talkobj.delete_talk_timer_id = timer.settimer(timeout * 100, "real_delete_talk")
    end 
end


--[Comment]
-- 真正删除桌子
--
function CMD.real_delete_talk(timerid, request)
    if talkobj.delete_talk_timer_id == timerid then
        talkobj.delete_talk_timer_id = -1
        skynet.send(talkobj.talk_service,"lua", "delete")
    end 
end

--[Comment]
-- 断线超时
--
function CMD.disconnect_outtime(timerid, request)
    if not request or request.index == nil then
        return
    end

    local seat = tableobj.seats[request.index]
    if not seat then
        return
    end
    seat.disconnect_timer_id = -1

    local rid,coin = seat.rid,tablelogic.get_curmoney(seat)
    tablelogic.passive_standuptable(tableobj, {}, seat, EStandupReason.STANDUP_REASON_DISCONNECTED)	
    tablelogic.passive_leavetable(tableobj,rid,coin)
end

return TalkTimer